/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getGenerals;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Namespace;

/**
 * Command to find a ConnectableElement with a given name.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Context
 * <li>Name
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>ConnectableElement - will be null if not found.
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class FindConnectableElementCommand extends Command {
	private Namespace context;
	private String name;
	private ConnectableElement element;

	public FindConnectableElementCommand() {
	}

	public FindConnectableElementCommand(final Namespace context,
			final String name) {
		setContext(context);
		setName(name);
	}

	public void setContext(final Namespace context) {
		this.context = context;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public ConnectableElement getConnectableElement() {
		return element;
	}

	@Override
	public void execute() {
		if (name == null || name.trim().length() == 0)
			return; // nothing to do, keep result == null

		element = searchRecursive(context);
	}

	private ConnectableElement searchRecursive(Element context) {
		if (!(context instanceof Namespace))
			return null;

		List<?> members = ((Namespace) context).getOwnedMembers();
		for (Object member : members) {
			if (member instanceof ConnectableElement
					&& name.equals(((ConnectableElement) member).getName())) {
				return (ConnectableElement) member;
			}
		}

		if (context instanceof Classifier) {
			List<Classifier> generals = getGenerals((Classifier) context);
			for (Classifier general : generals) {
				ConnectableElement result = searchRecursive(general);
				if (result != null)
					return result;
			}
		}

		return searchRecursive(context.getOwner());
	}

	@Override
	public void undo() {
		element = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		context = null;
		name = null;
		element = null;
	}
}
